﻿Imports Entidad
Imports Negocio
Imports Utilitario
Public Class frmConsultaXOC

    Dim CIA As String = "01"
    Dim SUC As String = "01"
    Dim TDO As String = "OC"

    Dim servicio1 As New OrdenesCompraNegocio
    Dim servicio2 As New BarraNegocio
    Dim util As New Utiles

    Sub mostrarOrdenCompra(codOrden As String)
        Dim orden As OrdenCompra = servicio1.obtenerOrdenCompra(CIA, SUC, TDO, codOrden)

        If orden Is Nothing Then
            util.mensaje("Ocurrió un error de conexión. Intentelo nuevamente.")
            Exit Sub
        End If

        If orden.CIA = "-1" Then
            util.mensaje("No existe la orden de compra.")
            Exit Sub
        End If

        lblProveedor.Text = "(" & orden.PROV_Codigo & ") " & orden.PROV_Nombre
        lblFecLlegada.Text = orden.FechaLlegada
        lblFecRegistro.Text = orden.FechaRegistro
        lblCantItems.Text = orden.CANT_Items

        listarDetalleOrdenCompra(codOrden)
    End Sub

    Sub listarDetalleOrdenCompra(codOrden As String)
        Dim ds As DataSet = servicio1.obtenerOrdenesCompraDetalle(CIA, SUC, TDO, codOrden)

        If ds Is Nothing Then
            util.mensaje("Ocurrió un error al obtener el detalle. Intentelo nuevamente.")
            Exit Sub
        End If

        dgvOrdenCompra.Rows.Clear()

        For i = 0 To ds.Tables(0).Rows.Count - 1
            dgvOrdenCompra.Rows.Add(ds.Tables(0).Rows(i)("ART_Codigo").ToString.Trim, ds.Tables(0).Rows(i)("ART_Descripcion").ToString.Trim, ds.Tables(0).Rows(i)("UNM_Abreviatura").ToString.Trim,
                                    ds.Tables(0).Rows(i)("DPD_Cantidad").ToString.Trim, ds.Tables(0).Rows(i)("CondBarra").ToString.Trim, ds.Tables(0).Rows(i)("CondCB").ToString.Trim)
        Next

        If ds.Tables(0).Rows.Count > 0 Then
            dgvOrdenCompra.Rows(0).Selected = False
        End If
    End Sub

    Private Sub btnBuscar_Click(sender As System.Object, e As System.EventArgs) Handles btnBuscar.Click
        mostrarOrdenCompra(txtOrdenCompra.Text.Trim)
    End Sub

    Private Sub txtOrdenCompra_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtOrdenCompra.KeyDown
        If e.KeyValue = Keys.Enter Then
            mostrarOrdenCompra(txtOrdenCompra.Text.Trim)
        End If
    End Sub

    Private Sub dgvOrdenCompra_CellDoubleClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvOrdenCompra.CellDoubleClick
        Dim f As Integer = e.RowIndex

        If f > -1 Then
            Dim agregar As New frmImpresionBarras(dgvOrdenCompra.Item("Column1", f).Value)
            If agregar.ShowDialog = Windows.Forms.DialogResult.OK Then

            End If
        End If
    End Sub

    Private Sub dgvOrdenCompra_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvOrdenCompra.CellFormatting
        For i = 0 To dgvOrdenCompra.Rows.Count - 1
            If dgvOrdenCompra.Item("Column6", i).Value = "0" And dgvOrdenCompra.Item("Column4", i).Value = "0" And e.RowIndex = i Then
                e.CellStyle.BackColor = Color.IndianRed
            ElseIf dgvOrdenCompra.Item("Column6", i).Value = "0" And dgvOrdenCompra.Item("Column4", i).Value = "1" And e.RowIndex = i Then
                e.CellStyle.BackColor = Color.PaleGreen
            End If
        Next
    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub

    Private Sub btnImprimirTodo_Click(sender As System.Object, e As System.EventArgs) Handles btnImprimirTodo.Click
        For Each fila As DataGridViewRow In dgvOrdenCompra.Rows
            If fila.Cells("Column4").Value = "1" Then
                Dim cantidad As Integer = 20
                Dim ds As DataSet = servicio2.obtenerDatoBarraXNivel(fila.Cells("Column1").Value, "1")

                If ds.Tables(0).Rows.Count > 0 Then
                    If CInt(fila.Cells("Column5").Value) < cantidad Then
                        cantidad = CInt(fila.Cells("Column5").Value)
                    End If

                    Dim reporte As New rptDobleBarra
                    reporte.SetParameterValue("@barra", "*" & ds.Tables(0).Rows(0)("BAR_Codigo").ToString.Trim & "*")
                    reporte.PrintToPrinter(IIf(cantidad Mod 2 > 0, (cantidad + 1) / 2, cantidad / 2), False, 0, 0)
                    reporte.SetParameterValue("@barra", "")
                    reporte.PrintToPrinter(1, False, 0, 0)
                End If
            End If
        Next
    End Sub

End Class